import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class AuctionClient extends UnicastRemoteObject implements AuctionClientIntf {

	public AuctionClient() throws RemoteException {
		super();
	}

	public static void displayMainMenu() {

		System.out.println("\nMain Menu");
		System.out.println();
		System.out.println("type 'create' to create a new auction.");
		System.out.println("type 'bid' to bid for an existing auction item.");
		System.out
				.println("type 'list' to show all the currently available auction items.");
		System.out.println();

	}

	// some way of making a bid i.e call remote method
	public static void main(String[] args) {

		String reg_host = "localhost";
		int reg_port = 1099;

		if (args.length == 1) {
			reg_port = Integer.parseInt(args[0]);
		} else if (args.length == 2) {
			reg_host = args[0];
			reg_port = Integer.parseInt(args[1]);
		}

		try {
			// Create the reference to the remote object through the
			// rmiregistry
			AuctionItemStore auctionItemStore = (AuctionItemStore) Naming
					.lookup("rmi://" + reg_host + ":" + reg_port
							+ "/AuctionItemService");

			// user operation
			Scanner keyboard = new Scanner(System.in);
			String inputLine = "";
			ArrayList<Integer> myCreatedAuctions = new ArrayList<Integer>();

			while (!(inputLine).equalsIgnoreCase("exit")) {

				displayMainMenu();

				inputLine = keyboard.nextLine();

				if (inputLine.equalsIgnoreCase("create")) {
					
					//get name
					System.out.println("Enter auction item name:");
					String name = keyboard.nextLine();
					
					//get minimum bid price
					System.out.println("Enter auction item minimum bid price:");
					Integer minItemValue = Integer.parseInt(keyboard.nextLine());
					
					//get date and time
					System.out.println("Enter the date and time the auction for the item will end:");
					DateFormat fmt = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
					fmt.setLenient(false);
					inputLine = keyboard.nextLine();
					Date closingDate = null;
					try {
						closingDate = fmt.parse(inputLine);
					} catch (ParseException e) {
						System.out.println("Incorrect format.");
						e.printStackTrace();
					}
					
					// create new auction item
					myCreatedAuctions.add(auctionItemStore.createAuction(name, minItemValue, closingDate));
					System.out.println("myCreatedAuctions: ");
					System.out.print(myCreatedAuctions.toString());
				}

				if (inputLine.equalsIgnoreCase("bid")) {

					// bid for an existing auction item
					// show user a list of available auction items

					String choice = keyboard.nextLine();
					// get data
					auctionItemStore.performBid(this, auctionID, bidValue);

				}

				if (inputLine.equalsIgnoreCase("list")) {

					// display list of currently available auctions
					System.out.println(auctionItemStore.getList());
				}
			}

			keyboard.close();
			System.out.println("\nApplication terminated");

		} // Catch the exceptions that may occur - rubbish URL, Remote
			// exception
			// Not bound exception or the arithmetic exception that may
			// occur in
			// one of the methods creates an arithmetic error (e.g. divide
			// by zero)
		catch (MalformedURLException murle) {
			System.out.println();
			System.out.println("MalformedURLException");
			System.out.println(murle);
		} catch (RemoteException re) {
			System.out.println();
			System.out.println("RemoteException");
			System.out.println(re);
		} catch (NotBoundException nbe) {
			System.out.println();
			System.out.println("NotBoundException");
			System.out.println(nbe);
		} catch (java.lang.ArithmeticException ae) {
			System.out.println();
			System.out.println("java.lang.ArithmeticException");
			System.out.println(ae);
		}

	}
}
